package cn.com.smartdevices.bracelet.chart.base;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/* renamed from: cn.com.smartdevices.bracelet.chart.base.l */
public class ChartScroller { // C0756l
    public static final int f3962a = 1;
    private static final String f3963b = "Chart.Scroller";
    private static final int f3964c = 400;
    protected ScrollingListener f3965d;
    private Context f3966e;
    private GestureDetector f3967f;
    protected Scroller f3968g;
    protected int f3969h;
    protected float f3970i;
    protected int f3971j;
    protected float f3972k;
    protected boolean f3973l;
    protected boolean f3974m;
    protected Handler f3975n;
    protected SimpleOnGestureListener f3976o;
    protected final int f3977p;
    protected final int f3978q;

    public ChartScroller(Context context, ScrollingListener c0735o) {
        this.f3976o = new C0758n(this);
        this.f3977p = 0;
        this.f3978q = f3962a;
        this.f3966e = context;
        this.f3965d = c0735o;
        this.f3967f = new GestureDetector(context, this.f3976o);
        this.f3967f.setIsLongpressEnabled(false);
        this.f3968g = new Scroller(context);
        this.f3975n = new C0757m(this);
    }

    class C0758n extends SimpleOnGestureListener {
        final /* synthetic */ ChartScroller f3980a;

        C0758n(ChartScroller c0756l) {
            this.f3980a = c0756l;
        }

        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            this.f3980a.f3969h = 0;
            this.f3980a.f3971j = 0;
            this.f3980a.f3968g.fling(this.f3980a.f3969h, this.f3980a.f3971j, (int) (-f), (int) (-f2), -5400, 5400, -5400, 5400);
            this.f3980a.m8651c(0);
            return true;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            return true;
        }
    }

    class C0757m extends Handler {
        final /* synthetic */ ChartScroller f3979a;

        C0757m(ChartScroller c0756l) {
            this.f3979a = c0756l;
        }

        public void handleMessage(Message message) {
            this.f3979a.f3968g.computeScrollOffset();
            int currX = this.f3979a.f3968g.getCurrX();
            int b = this.f3979a.f3969h - currX;
            this.f3979a.f3969h = currX;
            if (b != 0) {
                this.f3979a.f3965d.onScrollX(b);
            }
            b = this.f3979a.f3968g.getCurrY();
            int d = this.f3979a.f3971j - b;
            this.f3979a.f3971j = b;
            if (d != 0) {
                this.f3979a.f3965d.onScrollY(d);
            }
            if (this.f3979a.f3974m && Math.abs(currX - this.f3979a.f3968g.getFinalX()) < 1) {
                this.f3979a.f3968g.getFinalX();
                this.f3979a.f3968g.forceFinished(true);
            }
            if (!this.f3979a.f3974m && Math.abs(b - this.f3979a.f3968g.getFinalY()) < 1) {
                this.f3979a.f3968g.getFinalY();
                this.f3979a.f3968g.forceFinished(true);
            }
            if (!this.f3979a.f3968g.isFinished()) {
                this.f3979a.f3975n.sendEmptyMessage(message.what);
            } else if (message.what == 0) {
                this.f3979a.m8654d();
            } else {
                this.f3979a.m8659a();
            }
        }
    }

    public interface ScrollingListener  { // C0735o
        void onStart(); // m8487a

        boolean onScrollX(int i); //m8488a

        void onJustify(); // m8489b

        boolean onScrollY(int i);

        void onFinish(); // m8491c
    }

    private void m8650c() {
        this.f3975n.removeMessages(0);
        this.f3975n.removeMessages(f3962a);
    }

    protected void m8651c(int i) {
        m8650c();
        this.f3975n.sendEmptyMessage(i);
    }

    protected void m8654d() {
        this.f3965d.onJustify();
        m8651c((int) f3962a);
    }

    private void m8655e() {
        if (!this.f3973l) {
            this.f3973l = true;
            this.f3965d.onStart();
        }
    }

    void m8659a() {
        if (this.f3973l) {
            this.f3965d.onFinish();
            this.f3973l = false;
        }
    }

    public void scrollX (int i) { // m8660a
        scrollX(i, 0);
    }

    public void scrollX (int i, int i2) { // m8661a
        this.f3974m = true;
        this.f3968g.forceFinished(true);
        this.f3969h = 0;
        this.f3968g.startScroll(0, 0, i, 0, i2 != 0 ? i2 : f3964c);
        m8651c(0);
        m8655e();
    }

    public void setInterpolator (Interpolator interpolator) { //m8662a
        this.f3968g.forceFinished(true);
        this.f3968g = new Scroller(this.f3966e, interpolator);
    }

    public boolean onTouchEvent (MotionEvent motionEvent) { // m8663a
        boolean z;
        switch (motionEvent.getAction()) {
            case 0 /*0*/:
                this.f3970i = motionEvent.getX();
                this.f3972k = motionEvent.getY();
                this.f3968g.forceFinished(true);
                m8650c();
                z = true;
                break;
            case 2 /*2*/:
                int x = (int) (motionEvent.getX() - this.f3970i);
                int y = (int) (motionEvent.getY() - this.f3972k);
                if (x != 0) {
                    m8655e();
                    z = this.f3965d.onScrollX(x);
                    this.f3970i = motionEvent.getX();
                } else {
                    z = true;
                }
                if (y != 0) {
                    m8655e();
                    z = this.f3965d.onScrollY(y);
                    this.f3972k = motionEvent.getY();
                    break;
                }
                break;
            default:
                z = true;
                break;
        }
        if (!this.f3967f.onTouchEvent(motionEvent) && motionEvent.getAction() == f3962a) {
            m8654d();
        }
        return z;
    }

    public void forceFinishScrolling () { // m8664b
        if (!this.f3968g.isFinished()) {
            this.f3968g.forceFinished(true);
        }
    }

    public void scrollY(int i) { //m8665b
        scrollY(i, 0);
    }

    public void scrollY (int i, int i2) { //m8666b
        this.f3974m = false;
        this.f3968g.forceFinished(true);
        this.f3971j = 0;
        this.f3968g.startScroll(0, 0, 0, i, i2 != 0 ? i2 : f3964c);
        m8651c(0);
        m8655e();
    }
}
